package com.mifengs.mall.ui.push;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.MqttMsg;
import com.mifengs.mall.ui.my.RedPacketActivity;
import com.mifengs.mall.util.ToIntentUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lynn on 2018/1/23.
 */

public class PushDialogActivity extends Activity {
    @BindView(R.id.img_background)
    ImageView mImgBackground;
    @BindView(R.id.del_btn)
    ImageButton mDelBtn;
    @BindView(R.id.h5_del_btn)
    ImageButton mH5DelBtn;
    @BindView(R.id.receive_btn)
    TextView receiveBtn;
    @BindView(R.id.share_reward_text)
    TextView shareRewardText;
    @BindView(R.id.llayout_12)
    LinearLayout llayout12;
    @BindView(R.id.img_title)
    ImageView imgTitle;
    @BindView(R.id.img_num)
    ImageView imgNum;
    @BindView(R.id.rl_layout)
    RelativeLayout rlLayout;
    @BindView(R.id.price_text)
    TextView priceText;
    @BindView(R.id.llayout_redpack)
    LinearLayout llayoutRedpack;
    private String mqttMessage;
    private MqttMsg.ResultsBean resultsBean;
    public static PushDialogActivity instance;

    public static PushDialogActivity getInstance() {
        if (instance == null) {
            synchronized (PushDialogActivity.class) {
                if (instance == null) {
                    instance = new PushDialogActivity();
                }
            }
        }
        return instance;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_push_dialog);
        ButterKnife.bind(this);
        mqttMessage = getIntent().getStringExtra("mqttMessage");
        if (mqttMessage != null) {
            MqttMsg mqttMsg = new Gson().fromJson(mqttMessage, MqttMsg.class);
            resultsBean = mqttMsg.getResults();
            // 弹窗推送类型，01，无金额红包弹窗，02，有金额红包弹窗 03，生肖卡弹窗
            switch (resultsBean.getPushType()) {
                case "01":  //无金额红包弹窗
                    Glide.with(PushDialogActivity.this).load(R.drawable.redpack_dialog).asGif().into(mImgBackground);
                    mH5DelBtn.setVisibility(View.GONE);
                    mDelBtn.setVisibility(View.VISIBLE);
                    shareRewardText.setVisibility(View.GONE);
                    llayout12.setVisibility(View.GONE);
                    imgNum.setVisibility(View.GONE);
                    imgTitle.setVisibility(View.GONE);
                    receiveBtn.setVisibility(View.GONE);
                    llayoutRedpack.setVisibility(View.GONE);
                    break;
                case "02"://有金额红包弹窗
                    mH5DelBtn.setVisibility(View.GONE);
                    mDelBtn.setVisibility(View.VISIBLE);
                    llayoutRedpack.setVisibility(View.VISIBLE);
                    Glide.with(PushDialogActivity.this).load(R.drawable.push_redpack).asGif().into(mImgBackground);
                    receiveBtn.setVisibility(View.GONE);
                    shareRewardText.setVisibility(View.VISIBLE);
                    llayout12.setVisibility(View.GONE);
                    try {
                        if(resultsBean.getAmount().contains("00")){
                            priceText.setText(Integer.parseInt(resultsBean.getAmount()));
                        }else{
                            priceText.setText(resultsBean.getAmount());
                        }
                    }catch (Exception e){
                        priceText.setText(resultsBean.getAmount());
                    }


                    imgTitle.setVisibility(View.VISIBLE);
                    imgTitle.setBackgroundResource(R.drawable.share_friends_reward);

//                    if(Integer.parseInt(resultsBean.getAmount())== 5){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.new_people_5).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.GONE);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.new_people_reward);
//
//                    }else   if(Integer.parseInt(resultsBean.getAmount())== 8){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.share_reward).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.VISIBLE);
//                        imgNum.setBackgroundResource(R.drawable.jinbi_8);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.share_friends_reward);
//                    }else   if(Integer.parseInt(resultsBean.getAmount())== 28){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.share_reward).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.VISIBLE);
//                        imgNum.setBackgroundResource(R.drawable.jinbi_28);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.share_friends_reward);
//
//                    }else   if(Integer.parseInt(resultsBean.getAmount())== 58){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.share_reward).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.VISIBLE);
//                        imgNum.setBackgroundResource(R.drawable.jinbi_58);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.share_friends_reward);
//
//                    }else   if(Integer.parseInt(resultsBean.getAmount())== 98){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.share_reward).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.VISIBLE);
//                        imgNum.setBackgroundResource(R.drawable.jinbi_98);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.share_friends_reward);
//
//                    }else   if(Integer.parseInt(resultsBean.getAmount())== 218){
//                        Glide.with(PushDialogActivity.this).load(R.drawable.share_reward).asGif().into(mImgBackground);
//                        shareRewardText.setVisibility(View.VISIBLE);
//                        llayout12.setVisibility(View.GONE);
//                        imgNum.setVisibility(View.VISIBLE);
//                        imgNum.setBackgroundResource(R.drawable.jinbi_218);
//                        imgTitle.setVisibility(View.VISIBLE);
//                        imgTitle.setBackgroundResource(R.drawable.share_friends_reward);
//
//                    }
                    break;
                case "03"://生肖卡弹窗
                    Glide.with(PushDialogActivity.this).load(R.drawable.new_people_12).asGif().into(mImgBackground);
                    mH5DelBtn.setVisibility(View.GONE);
                    mDelBtn.setVisibility(View.VISIBLE);
                    shareRewardText.setVisibility(View.GONE);
                    llayout12.setVisibility(View.VISIBLE);
                    imgNum.setVisibility(View.GONE);
                    imgTitle.setVisibility(View.VISIBLE);
                    imgTitle.setBackgroundResource(R.drawable.new_people_reward);
                    llayoutRedpack.setVisibility(View.GONE);
                    break;

            }
//                ImageLoadUtil.displayImage(PushDialogActivity.this, resultsBean.getImageurl().trim(), mImgBackground);
//                mH5DelBtn.setVisibility(View.VISIBLE);
//                mDelBtn.setVisibility(View.GONE);
        }

    }

    @OnClick({R.id.rl_layout, R.id.del_btn, R.id.h5_del_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.del_btn:
                finish();
                break;
            case R.id.rl_layout:
                if (resultsBean != null) {
                    if (resultsBean.getType().equals("03")) {
                        ToIntentUtil.toCommonIntent(resultsBean.getUrl(), this);
                    } else {
                        startActivity(new Intent(PushDialogActivity.this, RedPacketActivity.class));
                    }
                    finish();
                }
                break;
//            case R.id.img_background:
//                if (resultsBean != null) {
//                    if (resultsBean.getType().equals("03")) {
//                        ToIntentUtil.toCommonIntent(resultsBean.getUrl(), this);
//                    } else {
//                        startActivity(new Intent(PushDialogActivity.this, RedPacketActivity.class));
//                    }
//                    finish();
//                }
//                break;
            case R.id.h5_del_btn:
                finish();
                break;
        }
    }
}
